package study.config.apollo.support.bean;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Set;

@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "prop-v1")
public class Bean1 {

    private String name;

    private Integer age;

    @ApolloConfigChangeListener(value = "config.yml", interestedKeyPrefixes = "prop-v1.")
    public void onChange(ConfigChangeEvent changeEvent) {
        log.info("Apollo配置发生改变");
        Set<String> changedKeys = changeEvent.changedKeys();
        changedKeys.stream()
                .forEach(key -> {
                    ConfigChange change = changeEvent.getChange(key);
                    log.info("Apollo change: {}= {}", key, change.getNewValue());
                });
        //rebind configuration beans（配置类无需要添加@RefreshScope）
//        context.publishEvent(new EnvironmentChangeEvent(changedKeys));
    }
}
